//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')

import tableList from '../../components/modules/tablelist'
// import mock from './data';
let mkModal = require('../../components/modules/modal')

const headData = [
  { title: '截止日期', key: 'time', hasClick: true},
  { title: '是否经过审计', key: 'isSJ', hasClick: true},
  { title: '审计日期', key: 'sjTime', align: 'right', hasClick: true},
]
const data = [
  {time: '2020/3/31', isSJ: '未审计', sjTime: '2020/02/02'},
  {time: '2020/3/31', isSJ: '已审计', sjTime: '2020/02/02'},
  {time: '2020/3/31', isSJ: '未审计', sjTime: '2020/02/02'},
]



const data2 = [
  {name: '财务报表截止日期', column1: '2020/01/02', column2: '2020/01/02', column3: '2020/01/02', column4: '2020/01/02'},
  {name: '是否经过审计', column1: '未审计', column2: '未审计', column3: '已审计', column4: '已审计'},
  {name: '审计日期', column1: '2020/01/02', column2: '2020/01/02', column3: '2020/01/02', column4: '2020/01/02'},
  {name: '财务报表类型', column1: '1', column2: '1', column3: '2', column4: '2'},
]
const headData2 = [
  {
    title: {
      '@item': {
        title: [
          { title: '截止日期' },
          { title: '指标' }
        ],
        titleClass: 'lineTd'
      }
    },
    key: 'name',
    width: '260',
    sticky: true,
  },
  {
    title: '2020/01/02',
    key: 'column1',
    width: '200',
    hasClick: true
  },
  {
    title: '2020/01/03',
    key: 'column2',
    width: '200',
    hasClick: true
  },
  {
    title: '2020/01/04',
    key: 'column3',
    width: '200',
    hasClick: true
  },
  {
    title: '2020/01/05',
    key: 'column4',
    width: '200',
    hasClick: true
  },
]
const data3 = [
  {name: {value: '财务报表截止日期', status: 0}, column1: {value: '2020/01/02', status: 0}, column2: {value: '2020/01/02', status: 0}, column3: {value: '2020/01/02', status: 0}, column4: {value:'2020/01/02', status: 0},},
  {name: {value: '是否经过审计', status: 0}, column1: {value: '未审计', status: 0}, column2: {value: '未审计', status: 0}, column3: {value: '已审计', status: 0}, column4: {value:'已审计', status: 0},},
  {name: {value: '审计日期', status: 0}, column1: {value: '2020/01/02', status: 1}, column2: {value: '2020/01/02', status: 0}, column3: {value: '2020/01/02', status: 0}, column4: {value:'2020/01/02', status: 0},},
  {name: {value: '财务报表类型', status: 0}, column1: {value: '1', status: 0}, column2: {value: '1', status: 0}, column3: {value: '2', status: 0}, column4: {value:'2', status: 0},},
]
const headData3 = [
  {
    title: {
      title: [
        { title: '截止日期' },
        { title: '指标' }
      ],
      titleClass: 'lineTd'
    },
    key: 'name',
    width: '260',
    sticky: true,
  },
  {
    title: '2020/01/02',
    key: 'column1',
    width: '200',
    hasClick: true
  },
  {
    title: '2020/01/03',
    key: 'column2',
    width: '200',
    hasClick: true
  },
  {
    title: '2020/01/04',
    key: 'column3',
    width: '200',
    hasClick: true
  },
  {
    title: '2020/01/05',
    key: 'column4',
    width: '200',
    hasClick: true
  },
]

const mock2 = {
  headData: ["2021-12-31", "2021-12-30", "2021-12-29", "2021-12-29"],
  data: [
    {
      code: 'a',
      name: '经营活动产生的现金流量',
      value: [],
      isLevel1: true,
      isEdit: false,
    },
    {
        code: 'a1',
        name: '销售商品',
        value: ['a1v', 'a2v', 'a3v', 'a4v'],
        isLevel1: false,
      },
      {
        code: 'a2',
        name: '提供劳务收到的现金',
        value: ['a1v', 'a2v', '提供劳务收到的现金', 'a4v'],
        isLevel1: false,
      },
      {
        code: 'a3',
        name: '提供劳务收到的现金',
        value: ['a1v', 'a2v', 'a3v', 'a4v'],
        isLevel1: false,
      },
    {
      code: 'b',
      name: '经营活动产生的现金流量',
      value: ['', '', '', ''],
      isLevel1: true,
      isEdit: true,
    },
    {
        code: 'b1',
        name: '销售商品',
        value: ['b1v', null, 'b3v', 'b4v'],
        isLevel1: false,
      },
      {
        code: 'b2',
        name: '提供劳务收到的现金',
        value: ['b1v', 'b2v', 'b3v', 'b4v'],
        isLevel1: false,
      },
      {
        code: 'b3',
        name: '提供劳务收到的现金',
        value: ['b1v', 'b2v', 'b3v', 'b4v'],
        isLevel1: false,
      },
  ]
}




Pager({
  data: {
    tableInst: tableList({
      headData: headData,
      data: data,
      tableClass: 'border mb-default',
      // isHideHeader: true,
      // unit: '%',
      autoWidth: true
    }),
    tableInst2: {$$id: 'tableInst2'},
    modal: {$$id: 'modal'},
    tableWxml: {
      headData: headData,
      data: data,
      tableClass: 'border mb-default'
    },
    tableWxml2: {
      headData: headData2,
      data: data2,
      tableClass: 'auto border mb-default',
      itemStyle: ''
    },

    tableInst3: null,
    tableInst4: null,
    tableInst5: null,
  },
  onLoad() {
  },
  onReady() {
    const $tableInst2 = this.getElementsById('tableInst2')
    this.$modal = this.getElementsById('modal')
    this.vm = null
    const that = this

    const inst = tableList({
      headData: headData2,
      data: data2,
      tableClass: 'auto border sticky mb-default',
      onItemClick(params) {
        that.vm = this      // 组件内部的this
        that.openModal(params)
      },
    })
    $tableInst2.update(inst)
    
    // mock.headData.unshift({
    //   title: '财务报表截止日期',
    //   key: 'name',
    //   width: '260',
    //   sticky: true,
    // })
    let sumWidth = 0
    mock2.headData.forEach(item => {
      sumWidth += parseInt(item.width || 220)
    })
    console.log(sumWidth, 'sumWidth')
    this.setData({
      tableInst3: {
        headData: headData,
        data: [],
        tableClass: 'border auto mb-default',
      },
      tableInst4: {
        headData: headData3,
        data: data3,
        tableClass: 'border auto mb-default',
        onItemClick(params) {
          const {idx, key, value} = params
          const dt = this.data.data
          const abc = that.filterClas(dt, params)
          this.setData({data: abc})
          that.vm1 = this
          that.openModal(params, 1)
        },
      },
      tableInst5: {
        headData: mock2.headData,
        data: mock2.data,
        sumWidth: sumWidth
      },
    })
  },
  filterClas(data, params = {}) {
    const {idx, key, value} = params
    const dt = data.map((item, index) => {
      const aa = this.findCls(item, 'active')
      if (aa) item[aa[0]][aa[1]] = ''
      if (index === idx) {
        item[key]['itemClass'] = 'active'
      }
      return item
    })
    return dt
  },

  findCls(object, value) {
    for (var key in object) {
      if (object[key] == value) return [key];
      if (typeof(object[key]) == "object") {
        var temp = this.findCls(object[key], value);
        if (temp) return [key, temp].flat();
      }
    }
  },
  openModal(params, status) {
    const {value, idx, key} = params
    this.$modal.update(mkModal({
      data: {
        title: {title: '修改', itemClass: 'ss-center title-default'},
        '@form': {
          $$id: 'reviewQuota',
          data: [{input: [{type: 'text', placeholder: '请输入', id: 'text', value: value, inputClass: 'input-border'}]}],
          formClass: 'wid-p100 mtb-minor'
        },
        footer: [ { title: '提交', itemClass: 'ss-button btn-default', aim: 'onSubmitModal?key='+key +'&idx='+idx + '&status=' + status} ],
        footerClass: 'mt50',
        itemClass: 'modal-wrap'
      },
      close: true,
      // direction: 'bottom',
      type: 'modal',
      // animate: false,
      actionSideWrapClass: 'pages-modal-middle active',
      actionSideClass: 'wid-p80'
    }))
  },
  onSubmitModal(e, params, inst) {
    const {key, idx, status} = params
    const $formInst = this.getElementsById('reviewQuota')
    const value = $formInst.getValue('text')
    this.$modal.onHide()
    if (status !== '2') {
      const dt = this.vm1.data.data
      const hasClas = this.filterClas(dt)
      if (status === '1') {
        this.vm1.setData({
          data: hasClas
        }, () => {
          this.vm1.setData({
            [`data[${idx}]${key}.value`]: value
          })
        })
      }
      else {
        data2[idx][key] = value
        this.vm.update({title: value})
      }
    }
    else {
      wx.showToast({title: '待返回数据，刷新页面', icon: 'none'})
    }
  },
  onTd(e) {
    const {month, code, value} = e.currentTarget.dataset
    const params = {
      value: value,
      key: code,
      idx: month
    }
    this.openModal(params, 2)
  }
})

// const xxx = new Map([
//   ['aaa', {}],
//   ['bbb', 'jfkdjfsdf'],
//   ['ccc', 'jfkdjfsdf'],
//   ['ddd', 'jfkdjfsdf'],
//   ['eee', 'jfkdjfsdf'],
//   ['fff', 'jfkdjfsdf'],

// ])

// xxx.get('fff')
// xxx.set(['eee', 'fjkdsjfksg'],)
// xxx.keys()
// xxx.values()